﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAO
{
    public class DataAccess
    {
        protected static String _connectionString = ConfigurationSettings.AppSettings["connectionStrings"];
        static SqlConnection connection = null;

        public static void OpenConnection()
        {
            connection = new SqlConnection(_connectionString);
            connection.Open();
        }

        public static void CloseConnection()
        {
            if (connection != null)
                connection.Close();
        }

        public static DataTable ExecQuery(string sql)
        {
            OpenConnection();
            DataTable dt = new DataTable();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = sql;
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
        }

        public static void ExecNonQuery(string sql)
        {
            OpenConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = sql;
            command.ExecuteNonQuery();
            CloseConnection();
        }

        public static object ExecScalar(string sql)
        {
            OpenConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandText = sql;
            object o = command.ExecuteScalar();
            CloseConnection();
            return o;
        }

    }
}
